package com.flyway.handles;

import com.flyway.utils.R;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 全局异常处理器
 * @Author: xupx
 * @Date: 2019/11/15
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {


    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public R exception(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        return R.error(allErrors.get(0).getDefaultMessage());
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R exception(Exception e) {
        e.printStackTrace();
        return R.error();
    }
}